package scct.jiangjiale.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.*;
import scct.jiangjiale.dao.*;
import scct.jiangjiale.model.User;
/*
 * Created by JFormDesigner on Mon Jun 24 19:27:51 CST 2024
 */



/**
 * @author 蒋家乐
 */
public class test extends JFrame {
    public test() {
        initComponents();
    }

    private void thisWindowGainedFocus(WindowEvent e) {
        // TODO add your code here
    }

    private void ok(ActionEvent e) {
        // TODO add your code here
        String name =txtName.getText();
        String pass = txtPass.getText();
        System.out.println(name +","+pass);
            if (!name.matches("^\\w{3,10}")){
                JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请检查输入");
                return;
            }
                if(!pass.matches("^\\w{3,10}")){
                    JOptionPane.showMessageDialog(null,"你输入的密码不合法，请检查输入");
                    return;
                }
        UserDao userDao =new UserDao();
          User user= userDao.login(name,pass);
            if (user==null){
                JOptionPane.showMessageDialog(this,"用户名或密码错误，请重新在试");
            }else {
                JOptionPane.showMessageDialog(this,"登录成功");
             FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
            formMain.setLocationRelativeTo(null);
            formMain.setVisible(true);
             this.dispose();
            }

    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 蒋家乐
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        label4 = new JLabel();
        label3 = new JLabel();
        txtPass = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowGainedFocus(WindowEvent e) {
                thisWindowGainedFocus(e);
            }
        });
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border.
            EmptyBorder( 0, 0, 0, 0) , "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn", javax. swing. border. TitledBorder. CENTER, javax. swing
            . border. TitledBorder. BOTTOM, new java .awt .Font ("Dia\u006cog" ,java .awt .Font .BOLD ,12 ),
            java. awt. Color. red) ,dialogPane. getBorder( )) ); dialogPane. addPropertyChangeListener (new java. beans. PropertyChangeListener( )
            { @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062ord\u0065r" .equals (e .getPropertyName () ))
            throw new RuntimeException( ); }} );
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u767b\u5f55");
                label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 10f));
                contentPanel.add(label1);
                label1.setBounds(175, 25, 47, label1.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u8d26\u53f7\uff1a");
                contentPanel.add(label2);
                label2.setBounds(65, 73, 45, label2.getPreferredSize().height);
                contentPanel.add(txtName);
                txtName.setBounds(117, 64, 182, txtName.getPreferredSize().height);

                //---- label4 ----
                label4.setText("\u5bc6\u7801\uff1a");
                contentPanel.add(label4);
                label4.setBounds(new Rectangle(new Point(65, 116), label4.getPreferredSize()));
                contentPanel.add(label3);
                label3.setBounds(new Rectangle(new Point(110, 125), label3.getPreferredSize()));
                contentPanel.add(txtPass);
                txtPass.setBounds(120, 105, 182, txtPass.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setLayout(new MigLayout(
                    "insets dialog,alignx right",
                    // columns
                    "[fill]" +
                    "[fill]" +
                    "[fill]" +
                    "[fill]" +
                    "[fill]" +
                    "[fill]" +
                    "[fill]" +
                    "[button,fill]" +
                    "[button,fill]",
                    // rows
                    "[]" +
                    "[]" +
                    "[]"));

                //---- okButton ----
                okButton.setText("\u767b\u5f55");
                okButton.addActionListener(e -> ok(e));
                buttonBar.add(okButton, "cell 3 0");

                //---- cancelButton ----
                cancelButton.setText("\u6ce8\u518c");
                buttonBar.add(cancelButton, "cell 7 0");
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 蒋家乐
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JLabel label4;
    private JLabel label3;
    private JTextField txtPass;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on

    public static void main(String[] args) {
                            test test=new test();
                 test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    new test().setVisible(true);
    }




}

 